import random
from PIL import Image
import requests
import io
import ddddocr

session = requests.Session()

slide = ddddocr.DdddOcr(det=False, ocr=False, show_ad=False)

value = random.random()


res = session.get(f'https://bm.ruankao.org.cn/sign/captchaImg?{value}')

print(session.cookies)

with open(f"./static/slide/three.png", "wb") as f:
    f.write(res.content)

three_img = Image.open(f"./static/slide/three.png")

# 背景图
background_img = three_img.crop((0, 0, 240, 150))
background_bytes = io.BytesIO()
background_img.save(background_bytes, format='PNG')

# background_img.save("./static/slide/background_img.png")
# background_img.show()

target_img = three_img.crop((0, 151, 50, 300))
target_bytes = io.BytesIO()
target_img.save(target_bytes, format='PNG')
# target_img.show()


# full_img = three_img.crop((0, 301, 240, 450))
# full_bytes = io.BytesIO()
# full_img.save(full_bytes, format='PNG')
# full_img.show()


res_slide = slide.slide_match(target_bytes.getvalue(), background_bytes.getvalue())
print(f"边缘检测", res_slide['target'][0])
offset = res_slide['target'][0]

# res_slide = slide.slide_comparison(background_bytes.getvalue(), full_bytes.getvalue())
# print(f"图像对比", res_slide['target'][0])



# 2. 开始验证
res = session.get(f'https://bm.ruankao.org.cn/sign/checkTnCode?p={offset}')
print(res.json())
print(session.cookies)


# 3. 开始请求
res  = session.post("https://bm.ruankao.org.cn/sign/inok", data={
"s1":"NDEwNTI3MTk5MDEyMjM0NDIzNA==",
"s2":"350c9d901b7f1c73a481e240ec1b7c0b",
"s3":offset
})

print(res.json())